var app = getApp()
Page({
  data: {
    region:["北京市","北京市","东城区"]
  }, 
  onLoad: function (e) {
    var that = this;
  },
  chooseLocation: function () {
    var that = this;
    wx.chooseLocation({
      success: (res) => {
        console.log(res);
        that.setData({
          curChooseAddress: res.name ? res.name : res.address,
          curChooseLatitude: res.latitude,
          curChooseLongitude: res.longitude
        })
      },
      fail:(res)=>{
        wx.getSetting({
          success: (res) => {
            if (!res.authSetting['scope.userLocation']) {
              that.setData({
                isShowsetting: true
              })
            } else {
              that.setData({
                locationTips: "无法获取您的当前定位地址，请打开定位权限"
              })
            }
          }
        })
      }
    })
  },
  hideSetting: function () {
    var that = this;
    that.setData({
      isShowsetting: false
    })
  },
  openSetting: function () {
    var that = this;
    that.hideSetting();
    wx.openSetting({
      success: (res) => {
        res.authSetting = {
          "scope.userLocation": true
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  inputChange:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      [type]:e.detail.value
    })
  },
  saveAddress: function () {
    var that = this;
    var region = that.data.region;
    var data = {};
    data.map = 'applet_address_add';
    data.name = that.data.name ? that.data.name:'';
    data.mobile = that.data.mobile ? that.data.mobile:'';
    data.pro = region[0];
    data.city = region[1];
    data.area = region[2];
    data.pcda = that.data.curChooseAddress?that.data.curChooseAddress:'';
    data.lat = that.data.curChooseLatitude?that.data.curChooseLatitude:'';
    data.lng = that.data.curChooseLongitude?that.data.curChooseLongitude:'';
    data.address = that.data.addressdetail ? that.data.addressdetail : '';
    data.code = that.data.code ? that.data.code:'';
    if (!data.name) {
      app.errorTip(that, '请输入姓名', 2000);
      return;
    }
    if (!data.mobile) {
      app.errorTip(that, '请输入正确的手机号', 2000);
      return;
    }
    if (!data.pcda) {
      app.errorTip(that, '请选择地址信息', 2000);
      return;
    }
    if (!data.pro || !data.city || !data.area) {
      app.errorTip(that, '请选择省市区', 2000);
      return;
    }
    if (!data.address) {
      app.errorTip(that, '请输入详细地址', 2000);
      return;
    }
    console.log(data);
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          wx.showToast({
            title: res.data.data.msg,
            icon: 'success',
            duration: 2000,
            success: function () {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.em,
            showCancel: false
          });
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
})